package com.gem.growingtree.controller;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.gem.growingtree.model.PageBean2;
import com.gem.growingtree.model.ShowTeachers;
import com.gem.growingtree.service.TeacherAccountService;

@Controller
public class TeacherAccountController {
	@RequestMapping("/teacherAccount")
	public ModelAndView ShowTeacherAccount(HttpServletRequest request, HttpServletResponse response){
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		TeacherAccountService pas = new TeacherAccountService();
		try {
			PageBean2 pb = pas.findByPage(pageCode, pageSize);
			request.setAttribute("pageBean2", pb);
			return new ModelAndView("/teachers");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	// 查找某个老师信息
	@RequestMapping("/showCertainTeacher")
	public ModelAndView ShowTeacherAccount2(HttpServletRequest request, HttpServletResponse response,String showT){
		//String userId = request.getParameter("userId");
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		TeacherAccountService pas = new TeacherAccountService();
		try {
			PageBean2 pb = pas.findByPage2(pageCode, pageSize,showT);
			request.setAttribute("pageBean2", pb);
			return new ModelAndView("/teachers");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//显示要修改的老师信息
	@RequestMapping("/showCertainTeacher3")
	public ModelAndView ShowTeacherAccount3(HttpServletRequest request, HttpServletResponse response,String showT) throws UnsupportedEncodingException{
		String showT1 = new  String(showT.getBytes("iso-8859-1"),"utf-8"); 
		int pageCode = 0;
		String pc = request.getParameter("pc");
		if (pc == null) {
			pageCode = 1;
		} else {
			pageCode = Integer.parseInt(pc);
		}
		int pageSize = 4;
		TeacherAccountService pas = new TeacherAccountService();
		try {
			PageBean2 pb = pas.findByPage2(pageCode, pageSize,showT1);
			request.setAttribute("pageBean2", pb);
			return new ModelAndView("/teacher");
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	//更新某个老师信息
	@RequestMapping("/modifyTeacher")
	public String modifyTeacher(HttpServletRequest request,
			HttpServletResponse response, ShowTeachers showTeachers) {
		String showT = request.getParameter("showT");
		System.out.println("获取到了老师名字为："+showT);
		if (showT != null) {
			try {
				TeacherAccountService teacherAccountService = new TeacherAccountService();
				teacherAccountService.updateTeacher(showT,showTeachers);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return "/teacher";
	}
	//删除某个老师的信息
	@RequestMapping("/deleteTeacher")
	public ModelAndView deleteTeacherInfo( HttpServletRequest request,
			HttpServletResponse response) throws Exception {	

		String  teacherId = request.getParameter("teacherId");
		TeacherAccountService tas = new TeacherAccountService();
		tas.deleteTeacherInfo(teacherId);
		int current = 0;
		PageBean2  pageBean2 = tas.findByPage(current, 4);
		request.setAttribute("pageBean2", pageBean2);
		return new ModelAndView("/teachers", "pageBean", pageBean2);
	}
}
